<?php

namespace app\admin\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use think\exception\HttpResponseException;


class Base extends  BaseController {
      
  
  public function initialize() {

    

   
   
      $loginAdmin =session('adminAccount');     
    if(!$loginAdmin) {
       header('Location: /admin/login');
    }
    
   View::assign('username', $loginAdmin['username']);   
   
 
  
  }

  public function redirect(...$args){
    throw new HttpResponseException(redirect(...$args));
  }
    


    //通用后台密码加盐
    public function password_salt($str){
      $salt='ddsEf6KJ';
      return md5($str.$salt);
    }
     public function qingqiu($url,$data){
      $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
   
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $shouquan = curl_exec($ch);
        curl_close($ch);
          return $shouquan;
    }



    //更改状态
    public function status() {

        $id = Request::instance()->param('id','intval');
        $status = Request::instance()->param('status','intval');
        $model = Request::instance()->param('model');

        if(empty($id)) {

            $this->error('id不合法');

        }

        if(!is_numeric($status)) {

            $this->error('status不合法');

        }


        $res = Db::name($model)->where('id',$id)->update(['status'=>$status]);

        if($res) {

            $this->success('更新成功');

        }else {

            $this->error('更新失败');

        }

    }



    /*通用删除逻辑*/

    public function del(){

      $id=Request::instance()->param('id');
      $model=Request::instance()->param('m');
      $status=Request::instance()->param('status');

      if(!intval($id)){

        return $this->error('ID不合法');

      }


      //如果状态为空则是直接删除，不为空则更新状态

      if(empty($status)){

        $res=Db::name($model)->where('id',$id)->delete();

      }else{

        $res=Db::name($model)->save(['status'=>$status],['id'=>$id]);

      }


      if($res){
        return alert('操作成功！','index',6,3);
     }else{
        return alert('操作失败！','index',5,3);
     }

    }



      /*

      **公共排序方法

      **需要在排序html中传入下面三个参数

      */

      public function listorder($id,$model,$listorder) {

        $data=input('post.');

        $res = Db::name($model)->where('id',$id)->update(['listorder'=>$listorder]);

        if($res) {

           return ['data'=>$_SERVER['HTTP_REFERER'],'code'=>1];

        }else {

          return['data'=>$_SERVER['HTTP_REFERER'],'code'=>0,'msg'=>'失败！'];

        }



    }
   

	    public function HttpPostCookie($url, $data,$timeout = 60)
    {
         $BT_CONFIG = Db::name('urlconfig')->where('id',1)->find();
         $BT_PANEL = $BT_CONFIG['mianbanurl'];
    	//定义cookie保存位置
     	//定义cookie保存位置
        $cookie_file='./'.md5($BT_PANEL).'.cookie';
        if(!file_exists($cookie_file)){
            $fp = fopen($cookie_file,'w+');
            fclose($fp);
        }
		
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
		public function GetKeyData(){
		$urlData=Db::name('urlconfig')->where('id',1)->find();    
  		$now_time = time();
  		$request_token = 	md5($now_time.''.md5($urlData['ks']));
    	$p_data = array(
			'request_token'	=>	$request_token,
			'request_time'	=>	$now_time
		);
    	return $p_data;    
    }
   public function urlsadd($addurl) {
  
	$urlData=Db::name('urlconfig')->where('id',1)->find();    
	
		$url=$urlData['mianbanurl'].'/site?action=AddDomain';
		$p_data = $this->GetKeyData();		//取签名
	
		$p_data['id'] = $urlData['siteid'];
		$p_data['webname'] = $urlData['webname'];
		$p_data['domain'] = $addurl;

	$result = $this->HttpPostCookie($url,$p_data);
	
		//解析JSON数据
		$data = json_decode($result);
	
      	return $data;


	

	}
  public function urlsdel($delurl) {
  
	$urlData=Db::name('urlconfig')->where('id',1)->find();    
	
		$url=$urlData['mianbanurl'].'/site?action=DelDomain';
		$p_data = $this->GetKeyData();		//取签名
	
		$p_data['id'] = $urlData['siteid'];
		$p_data['webname'] = $urlData['webname'];
		$p_data['domain'] = $delurl;
     	$p_data['port'] = '80';
	$result = $this->HttpPostCookie($url,$p_data);
	
		//解析JSON数据
		$data = json_decode($result);
      	return $data;


	

	}


public function tongzhipost($post_data){
     $configData=Db::name('tongzhi')->where('id',1)->find();   
   $webhook = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=".$configData['wxkey'];   
# curl初始化
   $curl = curl_init();
#需要推送的url
curl_setopt($curl, CURLOPT_URL, $webhook);
curl_setopt($curl, CURLOPT_POST, 1);
#content必须是utf8编码
curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: application/json;charset=utf-8'));
#content 为需要推送的内容

curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$data = curl_exec($curl);
curl_close($curl);
    }
    
    



}